package com.example.demo.controller;


import com.example.demo.config.MqttBroker;
import com.example.demo.entity.ProductStore;
import com.example.demo.entity.PurchaseOrder;
import com.example.demo.entity.SerialNumber;
import com.example.demo.service.CodeHistoryService;
import com.example.demo.service.ProductInfoService;
import com.example.demo.service.ProductStoreService;
import com.example.demo.service.SerialNumberService;
import com.example.demo.util.GetMessage;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.RequiredArgsConstructor;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.*;

import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/productstore")
@RequiredArgsConstructor
@Component
public class ProductStoreController {
    private final ProductStoreService productStoreService;
    private final CodeHistoryService codeHistoryService;
    private final SerialNumberService serialNumberService;

    private static final ObjectMapper objectMapper = new ObjectMapper();
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @PostMapping("/store")
    public GetMessage store (@RequestBody ProductStore productStore,String userId,String trainingId) throws MqttException, JsonProcessingException {
        if(productStore.getOrdernumber()==null){
            return new GetMessage("未下单","500",null);
        }
        if(productStoreService.select(null,productStore.getOrdernumber())!=null){
            return new GetMessage("已入库","500",null);

        }
        String code=codeHistoryService.make(userId,"productStore",trainingId,2,null,"MA.156.110116.1007/61.12000000.2501");
        productStore.setIdentifycode(code);
        productStore.setStoretime(Util.getNowTime());
        productStoreService.save(productStore);

        Map<String,Object> map=new HashMap<>();
        map.put("type","putStore");
        map.put("trainingId",trainingId);
        map.put("data","");
        String payload = objectMapper.writeValueAsString(map);
        MqttBroker.mqttClient.publish("StepInfo/supply/"+userId,new MqttMessage(payload.getBytes(StandardCharsets.UTF_8)));
        return new GetMessage("成功","200",null);
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/select")
    public GetMessage select (String storeNumber,String orderNumber) throws JsonProcessingException, MqttException {
        ProductStore productStore=productStoreService.select(storeNumber,orderNumber);
        if(productStore!=null){
            return new GetMessage("成功","200",productStore);
        }
        return new GetMessage("失败","500","");
    }
    @CrossOrigin(origins ="*" ,maxAge = 3600)
    @GetMapping("/number")
    public GetMessage MakeNumber () throws JsonProcessingException, MqttException {
        SerialNumber serialNumber=serialNumberService.select("produceStore");
        String result="001";
        SerialNumber serialNumber1=new SerialNumber();
        if(serialNumber!=null){
            if(Util.checkTime(serialNumber.getUpdatetime())){
                //String[] str=serialNumber.getSerialnum().split("-");
                int num = Integer.parseInt(serialNumber.getSerialnum());
                num++;
                result = String.format("%03d", num);
                serialNumber1.setId(serialNumber.getId());
            }

        }
        serialNumber1.setSerialnum(result);
        serialNumber1.setUpdatetime(Util.getNowTime());
        serialNumber1.setType("produceStore");
        if(serialNumber!=null){
            serialNumber1.setId(serialNumber.getId());
            serialNumberService.updateById(serialNumber1);
        }else{
            serialNumberService.save(serialNumber1);
        }
        return new GetMessage("成功","200","KVH-RK-"+Util.getNowTimeStr()+"-"+result);
    }
}
